package mc.support.taobao.api;

import mc.support.taobao.api.fields.GetOnSaleItemFields;
import com.taobao.api.TaobaoRequest;
import com.taobao.api.request.ItemsOnsaleGetRequest;
import com.taobao.api.response.ItemsOnsaleGetResponse;

import java.util.Date;

/**
 * Created with IntelliJ IDEA. <br/>
 * User: francis    <br/>
 * Date: 13-11-7    <br/>
 * Time: 14:29  <br/>
 */
public class GetOnSaleItemReq extends AbstractReq<GetOnSaleItemReq, ItemsOnsaleGetResponse> {

    private GetOnSaleItemFields fields;
    private String q;
    private Long cid;
    private String[] sellerCids;
    private Long pageNo;
    private Boolean hasDiscount;
    private Boolean hasShowcase;
    private String orderBy;
    private Boolean isTaobao;
    private Boolean isEx;
    private Long pageSize;
    private Date startModified;
    private Date endModified;

    @Override
    protected TaobaoRequest toRequest() {
        ItemsOnsaleGetRequest req = new ItemsOnsaleGetRequest();

        if ( null != fields ) {
            req.setFields(fields.toString());
        }
        if ( null != q ) {
            req.setQ(q);
        }
        if ( null != cid ) {
            req.setCid(cid);
        }
        if ( null != sellerCids ) {
            req.setSellerCids(connect(sellerCids));
        }
        if ( null != pageNo ) {
            req.setPageNo(pageNo);
        }
        if ( null != hasDiscount ) {
            req.setHasDiscount(hasDiscount);
        }
        if ( null != hasShowcase ) {
            req.setHasShowcase(hasShowcase);
        }
        if ( null != orderBy ) {
            req.setOrderBy(orderBy);
        }
        if ( null != isTaobao ) {
            req.setIsTaobao(isTaobao);
        }
        if ( null != isEx ) {
            req.setIsEx(isEx);
        }
        if ( null != pageSize ) {
            req.setPageSize( pageSize );
        }
        if ( null != startModified ) {
            req.setStartModified( startModified );
        }
        if ( null != endModified ) {
            req.setEndModified( endModified );
        }
        return req;
    }

    private String connect(String[] ss) {
        StringBuilder s = new StringBuilder();

        for ( int i = 0; i < ss.length - 1; i++ ) {
            s.append( ss[i] ).append( ',' );
        }
        s.append( ss[ ss.length - 1 ] );

        return s.toString();
    }

    public GetOnSaleItemReq fields(GetOnSaleItemFields fields) {
        this.fields = fields;
        return this;
    }

    public GetOnSaleItemReq q(String q) {
        this.q = q;
        return this;
    }

    public GetOnSaleItemReq cid(Long cid) {
        this.cid = cid;
        return this;
    }

    public GetOnSaleItemReq sellerCids(String[] sellerCids) {
        this.sellerCids = sellerCids;
        return this;
    }

    public GetOnSaleItemReq pageNo(Long pageNo) {
        this.pageNo = pageNo;
        return this;
    }

    public GetOnSaleItemReq hasDiscount(Boolean hasDiscount) {
        this.hasDiscount = hasDiscount;
        return this;
    }

    public GetOnSaleItemReq hasShowcase(Boolean hasShowcase) {
        this.hasShowcase = hasShowcase;
        return this;
    }

    public GetOnSaleItemReq orderBy(String orderBy) {
        this.orderBy = orderBy;
        return this;
    }

    public GetOnSaleItemReq isTaobao(Boolean isTaobao) {
        this.isTaobao = isTaobao;
        return this;
    }

    public GetOnSaleItemReq isEx(Boolean isEx) {
        this.isEx = isEx;
        return this;
    }

    public GetOnSaleItemReq pageSize(Long pageSize) {
        this.pageSize = pageSize;
        return this;
    }

    public GetOnSaleItemReq startModified(Date startModified) {
        this.startModified = startModified;
        return this;
    }

    public GetOnSaleItemReq endModified(Date endModified) {
        this.endModified = endModified;
        return this;
    }

}
